package com.thunbu.thunbu_meeting_system.activity

import android.app.Activity
import android.content.Intent
import android.view.View
import com.cathu.lib_commonkt.base.BaseMVVMActivity
import com.hjq.toast.ToastUtils
import com.lcw.library.imagepicker.ImagePicker
import com.lxj.xpopup.XPopup
import com.thunbu.lib_common.utils.JsonUtils
import com.thunbu.lib_common.widget.CommonToolBar
import com.thunbu.thunbu_meeting_system.bean.DepartmentModel
import com.thunbu.thunbu_meeting_system.bean.StockModel
import com.thunbu.thunbu_meeting_system.databinding.ActivityInventoryAddBinding
import com.thunbu.thunbu_meeting_system.utils.TreeViewUtils
import com.thunbu.thunbu_meeting_system.view.imagePick.GlideLoader
import com.thunbu.thunbu_meeting_system.view.treeView.NodeModel
import com.thunbu.thunbu_meeting_system.view.treeView.TreeNode
import com.thunbu.thunbu_meeting_system.view.treeView.TreeViewPopupView
import com.thunbu.thunbu_meeting_system.viewMdel.InventoryAddVM
import com.wu.add.AddImagesInfo
import com.wu.add.AddImagesView


/**
 * @Description: 资产盘盈新增
 * @author: Yang.Yang33
 * @Date: 2023/3/21 9:48
 */
class InventoryAddActivity :BaseMVVMActivity<ActivityInventoryAddBinding, InventoryAddVM>(){
    lateinit var toolBar: CommonToolBar
    var typeNodes:MutableList<TreeNode<*>> = mutableListOf()
    var typeNodeid:Long? = null
    var userNodes:MutableList<TreeNode<*>> = mutableListOf()
    var userNodeid:Long? = null
    var departmentNodes:MutableList<TreeNode<*>> = mutableListOf()
    var departmentNodeid:Long? = null
    var addressNodes:MutableList<TreeNode<*>> = mutableListOf()
    var addressNodeid:Long? = null
    var assetStatus = 3 //资产类型,默认盘盈3
    lateinit var stockModel: StockModel


    override fun onMVVMCreated() {
        stockModel= JsonUtils.fromJson(intent.extras?.getString(InventoryActivity.STOCKTASK_CODE),StockModel::class.java)
        initToolBar()
        initView()
        initData()
        initVM()
    }

    private fun initVM() {
        vm.success.observe(this){
            setResult(Activity.RESULT_OK)
            finish()
        }
    }

    private fun initData() {
        TreeViewUtils.loadData(1,typeNodes)
        TreeViewUtils.loadData(2,userNodes)
//        TreeViewUtils.loadData(3,departmentNodes)
        TreeViewUtils.loadData(4,addressNodes)
    }

    private fun initView() {
        bindViews.addImages.setAddImagesViewListener(object : AddImagesView.AddImagesViewListener {
            override fun onAdd() {
                ImagePicker.getInstance()
//                    .setTitle("标题") //设置标题
                    .showCamera(true) //设置是否显示拍照按钮
                    .showImage(true) //设置是否展示图片
                    .showVideo(false) //设置是否展示视频
                    .setSingleType(true) //设置图片视频不能同时选择
                    .setMaxCount(9) //设置最大选择图片数目(默认为1，单选)
                    .setImageLoader(GlideLoader()) //设置自定义图片加载器
                    .start(
                        this@InventoryAddActivity,
                        200
                    ) //REQEST_SELECT_IMAGES_CODE为Intent调用的requestCode

            }

            override fun onPreview(item: AddImagesInfo?, lists: List<AddImagesInfo>?) {

            }
        })
        bindViews.rlModify.etType.setOnClickListener {
            showTreeViewPop(1,typeNodes)
        }
        bindViews.rlModify.etUser.setOnClickListener {
            showTreeViewPop(2,userNodes)
        }
        //部门由人员联动
        bindViews.rlModify.etDepartment.setOnClickListener {
//            showTreeViewPop(3,departmentNodes)
        }
        bindViews.rlModify.etDepartment.hint = ""
        bindViews.rlModify.ivDepartmentArraw.visibility = View.GONE
        bindViews.rlModify.etAddress.setOnClickListener {
            showTreeViewPop(4,addressNodes)
        }
        bindViews.tvSave.setOnClickListener {
            insertDiskSurplus()
        }
    }

    private fun insertDiskSurplus() {
        vm.insertDiskSurplus(assetStatus,stockModel.stockTaskCode,
//                typeNodeid?.toString(),
                bindViews.rlModify.etType.text?.toString(),
                bindViews.rlModify.etZc.text?.toString(),
                bindViews.rlModify.etBrand.text?.toString(),
                bindViews.rlModify.etModel.text?.toString(),
//                userNodeid?.toString(),
//                departmentNodeid?.toString(),
//                addressNodeid?.toString(),
                bindViews.rlModify.etUser.text?.toString(),
                bindViews.rlModify.etDepartment.text?.toString(),
                bindViews.rlModify.etAddress.text?.toString(),
                bindViews.rlModify.etRemark.text?.toString(),
                bindViews.rlModify.etShuliang.text?.toString(),
                bindViews.addImages.mAdapter?.getItems())
    }

    private fun showTreeViewPop(type:Int,nodes:MutableList<TreeNode<*>>) {
        if(nodes.isEmpty()){
            ToastUtils.show("接口数据为空,请返回首页重新获取")
            return
        }
        val treeViewPopupView = TreeViewPopupView(this)
        treeViewPopupView.setData(nodes) {
            //组织人员树的组织节点不做处理
            if(type == 2 && it.content is DepartmentModel && !(it.content as DepartmentModel).isUser){
                return@setData
            }
            treeViewPopupView.dismiss()
            val treeNode = it as TreeNode<NodeModel>
            when(type){
                1 ->  {
                    bindViews.rlModify.etType.setText(treeNode.content.name)
                    typeNodeid = treeNode.content.getmId()
                }
                2 ->  {
                    bindViews.rlModify.etUser.setText(treeNode.content.name)
                    userNodeid = treeNode.content.getmId()
                    bindViews.rlModify.etDepartment.setText((it.content as DepartmentModel).departmentName)
                }
//                3 ->  {
//                    bindViews.rlModify.etDepartment.setText(treeNode.content.name)
//                    departmentNodeid = treeNode.content.getmId()
//                }
                4 ->  {
                    bindViews.rlModify.etAddress.setText(treeNode.content.name)
                    addressNodeid = treeNode.content.getmId()
                }
            }

        }
        XPopup.Builder(this)
            .asCustom(treeViewPopupView)
            .show()
    }

    private fun initToolBar() {
        toolBar = setToolBar(CommonToolBar::class.java)
        toolBar.setBackAction { finish() }
        toolBar.setTitle("新增盘盈")
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == 200 && resultCode == RESULT_OK) {
            val imagePaths = data?.getStringArrayListExtra(ImagePicker.EXTRA_SELECT_IMAGES)
            var adds = ArrayList<AddImagesInfo>()
            imagePaths?.forEach {
                adds.add(AddImagesInfo(it, "2"))
            }
            bindViews.addImages.addAddImages(adds)
        }

    }
}